package beans;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import tlrs.Station;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

//import com.mysql.jdbc.Connection;

public class DbConnector implements Serializable {
	
	private static final long serialVersionUID = 3619404974818918453L;
	private Connection connection;
	public DbConnector()
	{
		Context inCon = null;
		try {
			inCon = new InitialContext();
			Context con = (Context) inCon.lookup("java:comp/env");
			DataSource data = (DataSource)con.lookup("jdbc/DB");
			System.out.println("den siger: " + data.toString());
			connection = data.getConnection();
			System.out.println("screw me");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public ArrayList<Station> getStations()
	{
		ArrayList<Station> returnList = new ArrayList<Station>();
		ResultSet res = null;
		try 
		{
			Statement statement = connection.createStatement();
			res = statement.executeQuery("SELECT * FROM Station");
			while(res.next())
			{
				Station s = new Station(res.getString(2), Integer.parseInt(res.getString(4)));
				returnList.add(s);
			}
		}
		catch (SQLException e)
		{
			
			e.printStackTrace();
		}
		
		return returnList;
	}
}
